package patrol.mobile.com.mobilepatrol.activity.culvert;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.realm.Realm;
import io.realm.RealmResults;
import patrol.mobile.com.mobilepatrol.PatrolApp;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.bean.Hddqjcb;
import patrol.mobile.com.mobilepatrol.listener.TimeListener;
import patrol.mobile.com.mobilepatrol.manager.HManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

import static patrol.mobile.com.mobilepatrol.R.array.hjsc;
import static patrol.mobile.com.mobilepatrol.R.array.hjsn;
import static patrol.mobile.com.mobilepatrol.R.array.yhc;
import static patrol.mobile.com.mobilepatrol.R.array.yhn;
import static patrol.mobile.com.mobilepatrol.R.id.et;
import static patrol.mobile.com.mobilepatrol.R.id.tv;

/**
 * Created by apple on 17/9/27.
 */

public class CulvertRegularCheckActivity extends BaseActivity {
//    @BindView(R.id.itme1)
//    RelativeLayout rl1;

    @BindView(R.id.itme2)
    RelativeLayout rl2;

    @BindView(R.id.itme3)
    RelativeLayout rl3;
    @BindView(R.id.itme4)
    RelativeLayout rl4;
    @BindView(R.id.itme5)
    RelativeLayout rl5;
    @BindView(R.id.itme6)
    RelativeLayout rl6;
    @BindView(R.id.itme7)
    RelativeLayout rl7;
    @BindView(R.id.itme8)
    RelativeLayout rl8;
    @BindView(R.id.itme9)
    RelativeLayout rl9;
    @BindView(R.id.itme10)
    RelativeLayout rl10;
    @BindView(R.id.itme11)
    RelativeLayout rl11;

    @BindView(R.id.itme12)
    RelativeLayout rl12;

    @BindView(R.id.itme13)
    RelativeLayout rl13;
    @BindView(R.id.itme14)
    RelativeLayout rl14;



    @BindView(R.id.bt_save)
    Button bt_save;

    @BindView(R.id.bt_list)
    Button bt_list;

//    private EditText et1;
    private EditText et2;
    private EditText et3;
    private EditText et4;
    private EditText et5;
    private EditText et6;
    private TextView tv_jcsj;
    private TextView tv_xcjcsj;
    private EditText et11;
    private EditText et12;
    private EditText et13;
    private EditText et14;


    private Hddqjcb hDQ;
    private String hddqjcid;
    private String hdcode;
    /** popup窗口里的ListView */
    private ListView jslv;
    private ListView yhlv;
    /** popup窗口 */
    private PopupWindow jsPopup;
    private PopupWindow yhPopup;

    /** 模拟的假数据 */
    private List<String> hjsdata;
    private List<String> hyhdata;

    /** 数据适配器 */
    private ArrayAdapter<String> jsAdapter;
    /** 数据适配器 */
    private ArrayAdapter<String> yhAdapter;
    private TextView tv_yh;
    private TextView tv_js;
    private String[] yhcs;
    private String[] jsc;
    private String yhcode="";
    private String yhname="";
    private String jscode="";
    private String jsname="";

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_culvert_regular_check;
    }

    @Override
    public void setListener() {
        rl7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.showDatePickDlg(CulvertRegularCheckActivity.this, new TimeListener() {
                    @Override
                    public void getDate(String date) {
                        tv_jcsj.setText(date);
                    }
                });
            }
        });
        rl10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.showDatePickDlg(CulvertRegularCheckActivity.this, new TimeListener() {
                    @Override
                    public void getDate(String date) {
                        tv_xcjcsj.setText(date);
                    }
                });
            }
        });
        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                final String id = et1.getText().toString().trim();
                final String roadcode = et2.getText().toString().trim();
                final String roadname = et3.getText().toString().trim();
                final String roadpos = et4.getText().toString().trim();
                final String gydw = et5.getText().toString().trim();
                final String hdlx = et6.getText().toString().trim();
                final String jcsj = tv_jcsj.getText().toString().trim();
                final String xcjcsj = tv_xcjcsj.getText().toString().trim();
                final String yhfazs = et11.getText().toString().trim();
                final String zgfzr = et12.getText().toString().trim();
                final String jcr = et13.getText().toString().trim();
                final String hdcode = et14.getText().toString().trim();

                PatrolApp.realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        RealmResults<Hddqjcb> br = realm.where(Hddqjcb.class).equalTo("id",hddqjcid).findAll();
                        Hddqjcb b = br.get(0);
//                        b.id = b.id;
                        b.roadcode = roadcode;
                        b.roadname = roadname;
                        b.roadpos = Double.valueOf(roadpos);
                        b.gydw = gydw;
                        b.hdlx = hdlx;
                        b.jcsj = jcsj;
                        b.hdjszk = jscode;
                        b.hdjszkname = jsname;
                        b.yhfa = yhcode;
                        b.yhfaname = yhname;
                        b.xcjcsj = xcjcsj;
                        b.yhfazs = yhfazs;
                        b.zgfzr = zgfzr;

                        b.jcr = jcr;
                        b.hdcode = hdcode;
                    }
                });

                HManager.setHDDQUpState(hddqjcid,false);
                HManager.setHDUpState(hdcode,false);
                showToast("保存成功");

            }
        });
        bt_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //检查桥梁id，定期检查id
                Intent intent = new Intent(CulvertRegularCheckActivity.this,CulvertRegularCheckBrokenListActivity.class);
                intent.putExtra("hddqjcid",hddqjcid);
                intent.putExtra("hdcode",hdcode);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(CulvertRegularCheckActivity.this,false);
            }
        });
        rl8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击控件后显示popup窗口
                initjs();
                // 使用isShowing()检查popup窗口是否在显示状态
                if (jsPopup != null && !jsPopup.isShowing()) {
                    jsPopup.showAsDropDown(tv_js, 0, 10);
                }
            }
        });
        rl9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击控件后显示popup窗口
                inityh();
                // 使用isShowing()检查popup窗口是否在显示状态
                if (yhPopup != null && !yhPopup.isShowing()) {
                    yhPopup.showAsDropDown(tv_yh, 0, 10);
                }
            }
        });
    }
    /**
     * 初始化popup窗口
     */
    private void inityh() {
        yhlv = new ListView(this);
        yhData();
        // 设置适配器
        yhAdapter = new ArrayAdapter<String>(this, R.layout.popup_text_item, hyhdata);
        yhlv.setAdapter(yhAdapter);

        // 设置ListView点击事件监听
        yhlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 在这里获取item数据
                yhname = hyhdata.get(position);
                // 把选择的数据展示对应的TextView上
                tv_yh.setText(yhname);
                yhcode = yhcs[position];
                // 选择完后关闭popup窗口
                yhPopup.dismiss();
            }
        });
        yhPopup = new PopupWindow(yhlv, tv_yh.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 取得popup窗口的背景图片
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg1);
        yhPopup.setBackgroundDrawable(drawable);
        yhPopup.setFocusable(true);
        yhPopup.setOutsideTouchable(true);
        yhPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 关闭popup窗口
                yhPopup.dismiss();
            }
        });
    }
    /**
     * 初始化popup窗口
     */
    private void initjs() {
        jslv = new ListView(this);
        jsData();
        // 设置适配器
        jsAdapter = new ArrayAdapter<String>(this, R.layout.popup_text_item, hjsdata);
        jslv.setAdapter(jsAdapter);

        // 设置ListView点击事件监听
        jslv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 在这里获取item数据
                jsname = hjsdata.get(position);
                // 把选择的数据展示对应的TextView上
                tv_js.setText(jsname);
                jscode = jsc[position];
                // 选择完后关闭popup窗口
                jsPopup.dismiss();
            }
        });
        jsPopup = new PopupWindow(jslv, tv_js.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 取得popup窗口的背景图片
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg1);
        jsPopup.setBackgroundDrawable(drawable);
        jsPopup.setFocusable(true);
        jsPopup.setOutsideTouchable(true);
        jsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 关闭popup窗口
                jsPopup.dismiss();
            }
        });
    }

    private void jsData() {
        Resources resources = getResources();
        jsc = resources.getStringArray(hjsc);
        String[] hjsns = resources.getStringArray(hjsn);
        hjsdata = new ArrayList<>();
        for (int i=0;i<hjsns.length;i++){
            hjsdata.add(hjsns[i]);
        }

    }
    private void yhData() {
        Resources resources = getResources();
        yhcs = resources.getStringArray(yhc);
        String[] yhns = resources.getStringArray(yhn);
        hyhdata = new ArrayList<>();
        for (int i=0;i<yhns.length;i++){
            hyhdata.add(yhns[i]);
        }

    }
    @Override
    public void initView() {
        super.initView();
//        TextView tv1 = rl1.findViewById(tv);
//        tv1.setText("涵洞定期检查id");
//        et1 = rl1.findViewById(et);

        TextView tv2 = rl2.findViewById(tv);
        tv2.setText("路线编码");
        et2 = rl2.findViewById(et);

        TextView tv3 = rl3.findViewById(tv);
        tv3.setText("路线名称");
        et3 = rl3.findViewById(et);

        TextView tv4 = rl4.findViewById(tv);
        tv4.setText("涵洞桩号");
        et4 = rl4.findViewById(et);

        TextView tv5 = rl5.findViewById(tv);
        tv5.setText("养护单位");
        et5 = rl5.findViewById(et);

        TextView tv6 = rl6.findViewById(tv);
        tv6.setText("涵洞类型");
        et6 = rl6.findViewById(et);

        TextView tv7 = rl7.findViewById(tv);
        tv7.setText("检查时间");
        tv_jcsj = rl7.findViewById(R.id.tv1);

        TextView tv8 = rl8.findViewById(tv);
        tv8.setText("涵洞技术状况");
        tv_js = rl8.findViewById(R.id.tv1);

        TextView tv9 = rl9.findViewById(tv);
        tv9.setText("养护方案");
        tv_yh = rl9.findViewById(R.id.tv1);

        TextView tv10 = rl10.findViewById(tv);
        tv10.setText("下次检查时间");
        tv_xcjcsj = rl10.findViewById(R.id.tv1);

        TextView tv11 = rl11.findViewById(tv);
        tv11.setText("养护方案综述");
        et11 = rl11.findViewById(et);

        TextView tv12 = rl12.findViewById(tv);
        tv12.setText("主管负责人");
        et12 = rl12.findViewById(et);

        TextView tv13 = rl13.findViewById(tv);
        tv13.setText("检查人");
        et13 = rl13.findViewById(et);

        TextView tv14 = rl14.findViewById(tv);
        tv14.setText("涵洞编号");
        et14 = rl14.findViewById(et);

    }

    @Override
    public void initData() {
        super.initData();
        hddqjcid = getIntent().getStringExtra("hddqjcid");
        hdcode = getIntent().getStringExtra("hdcode");
        hDQ = HManager.getCulvertDQ(hdcode,hddqjcid);

        setData();

    }

    private void setData() {
//        et1.setText(hDQ.id);

        et2.setText(hDQ.roadcode);

        et3.setText(hDQ.roadname);

        et4.setText(hDQ.roadpos+"");

        et5.setText(hDQ.gydw);

        et6.setText(hDQ.hdlx);

        tv_jcsj.setText(hDQ.jcsj);
        jscode = hDQ.hdjszk;
        jsname = hDQ.hdjszkname;
        tv_js.setText(hDQ.hdjszkname);
        yhcode = hDQ.yhfa;
        yhname = hDQ.yhfaname;
        tv_yh.setText(hDQ.yhfaname);

        tv_xcjcsj.setText(hDQ.xcjcsj);

        et11.setText(hDQ.yhfazs);

        et12.setText(hDQ.zgfzr);

        et13.setText(hDQ.jcr);

        et14.setText(hDQ.hdcode);

    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(CulvertRegularCheckActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("涵洞定期检查");
    }
}
